/*
    Bandleader
    Copyright (c) 2006 by Grant Yoshida

    Licensed under the GPL version 2 or later,
    please see COPYING for details.
*/

#ifndef ListSlicer_h
#define ListSlicer_h

#include <algorithm>

template <class T>
class ListSlicer
{
public:
    ListSlicer(const T &list, int startingIndex = 0) :
        m_list(list),
        m_index(startingIndex)
    {
    }

    //
    //! Returns a slice of the list. The slice has size elements.
    //
    T slice(int size)
    {
        T list;

        int listSize = m_list.size();
        int minIndex = std::min(m_index, listSize);
        int maxIndex = std::min(minIndex + size, listSize);

        for (int i = minIndex; i < maxIndex; ++i) {
            list.push_back(m_list[i]);
        }

        m_index = maxIndex;

        return list;
    }

    bool empty()
    {
        return m_index >= m_list.size();
    }

private:    
    const T &m_list;
    int m_index;
};

#endif
